#!/usr/bin/perl

# ---------- Init variables -------
#my $version = "0.0001";
# ---------- external readable parameters -------
## Functions
##-------------------------------------------------------
#
# stict syntax checking.
use strict;
#use CGI qw(param);
use SIM_cgi;

#BEGIN { $Exporter::Verbose=1 }

# Startupscript translater
my $module = $ARGV[0];
my $tool = $ARGV[1];
my $option1 = $ARGV[2];
my $option2 = $ARGV[3];
my $option3 = $ARGV[4];
my $option4 = $ARGV[5];
my $option5 = $ARGV[6];



my $backHeader="black";
my $httpUser = $ENV{'REMOTE_USER'};
my $httpReF = $ENV{'HTTP_REFERER'};
my $httpScriptName = $ENV{'SCRIPT_NAME'};
my $httpUserIp = $ENV{'REMOTE_ADDR'};
my $httpServerName = $ENV{'HTTP_HOST'};
my $httpCookie = $ENV{'HTTP_COOKIE'};
my $httpHost = $ENV{'HTTP_HOST'};
my $dloadDir = $ENV{'SIM_DLOADSTMPDIR'};
my $domainDir = $ENV{'SIM_DOMAINDIR'};
my $pluginDir = $ENV{'SIM_PLUGINSDIR'};

sub catFile {
  my $output = "";
  my ($file) = @_;
  open FH, $file or $output = "none";
# $output = readline FH;
  $output = <FH>;
  close FH;
  return $output;
}

sub catFileLine {
  my $outPut = "";
  my ($file) = @_;
  open FH, $file or $outPut = "$file";
  $outPut = <FH>;
  close FH;
  chomp($outPut);  # remove the newline
  return $outPut;
}

sub printFileLine {
  my $file = "@_[0]";
  my $varName = "@_[1]";
  my $rest = "@_[2]";
  my $logFile = "@_[3]";
  my $oldVal = catFileLine ("$file");
# print ("Update file=$file, varName=$varName, content:$rest<BR>");
  if ( "$oldVal" ne "$rest" ) {
    open(OUT, ">$file") || die "cannot create $file: $! (file=$file, varName=$varName, rest=$rest)";
    if ( "$logFile" ne "quiet" ) {
      print ("$varName is gewijzigd, oud:$oldVal nieuw:$rest<BR>");
    }
    print OUT $rest;
    close(OUT);
  }
}


sub testNameStrict {
  my $testname = "@_[0]";
  my $type = "@_[1]";
  if ( "$type" eq "word" ) {
    $testname =~ tr#a-zA-Z[\xE0][\xE8][\xEC][\xF2][\xF9]##cd;
  } else {
    $testname =~ tr#a-zA-Z0-9[\@][\.][\-]#A#cds;
  }
  #print ("testname=$testname");
  return $testname;
}

sub testMkDir {
  my $dirName = "@_[0]";
  if ( !-d $dirName ) {
    system "mkdir -p $dirName";
  }
}

sub expandIp {
  my $ipAddr = "@_[0]";
  my @pieces = split('\.', $ipAddr);
  my $returnIp;

  foreach my $chunk (@pieces) {
    my $eChunk = sprintf("%03d", $chunk);
    if ( "$returnIp" eq "" ) {
      $returnIp = "$eChunk";
    } else {
      $returnIp = "$returnIp.$eChunk";
    }
  }
  return $returnIp;
}

sub printHead {
  my $dispHead = "@_[0]";
  print("<FONT COLOR=#666666>");
  printMenu("extra","line", $module, $tool);
  print("
        </FONT>
	</TD></TR>
	</TABLE>
        ");
}

printHead;
#print("module=$module, tool=$tool, option1=$option1, option2=$option2, option3=$option3, option4=$option4.<BR>");
if ( "$tool" eq "vpn" ) {
  system("/usr/bin/perl $pluginDir/extra-vpn.pl $module $tool $option1 $option2 $option3 $option4");
} elsif ( "$tool" eq "starter" ) {
  system("/usr/bin/perl $pluginDir/extra-starter.pl $module $tool $option1 $option2 $option3 $option4");
} elsif ( "$tool" eq "reporter" ) {
  system("/usr/bin/perl $pluginDir/extra-reporter.pl $module $tool $option1 $option2 $option3 $option4 $option5");
} elsif ( "$tool" eq "syslog" ) {
  system("/usr/bin/perl $pluginDir/extra-syslog.pl $module $tool $option1 $option2 $option3 $option4 $option5");
} elsif ( "$tool" eq "userManager" ) {
  system("/usr/bin/perl $pluginDir/extra-userManager.pl $module $tool $option1 $option2 $option3 $option4 $option5");
} elsif ( "$tool" eq "tester" ) {
  system("$pluginDir/extra-tester.pl $module $tool $option1 $option2 $option3 $option4 $option5");
} elsif ( "$tool" eq "fixedInfo" ) {
  system("/usr/bin/perl $pluginDir/extra-fixedinfo.pl $module $tool $option1 $option2 $option3 $option4 $option5");
} elsif ( "$tool" eq "healthCheck" ) {
  system("$pluginDir/extra-healthcheck.pl $module $tool $option1 $option2 $option3 $option4");
} elsif ( "$tool" eq "trafficAnalyser" ) {
  system("$pluginDir/extra-trafficanalyser.pl $module $tool $option1 $option2 $option3 $option4 $option5");
} elsif ( "$tool" eq "remoteExec" ) {
  system("$pluginDir/extra-remoteExec.pl $module $tool $option1 $option2 $option3 $option4 $option5");
} elsif ( "$tool" eq "userSettings" ) {
  system("$pluginDir/extra-settings.pl $module $tool $option1 $option2 $option3 $option4");
} elsif ( "$tool" eq "adminSettings" ) {
  system("$pluginDir/extra-settings.pl $module $tool $option1 $option2 $option3 $option4");
} elsif ( "$tool" eq "import" ) {
  system("$pluginDir/extra-import.pl $module $tool $option1 $option2 $option3 $option4");
} elsif ( "$tool" eq "seedTool" ) {
  system("$pluginDir/extra-seedtool.pl $module $tool $option1 $option2 $option3 $option4");
} elsif ( "$tool" eq "admin" ) {
  system("$pluginDir/extra-admin.pl $module $tool $option1 $option2 $option3 $option4 2>&1");
} else {
  print("<CENTER><TABLE>");
  printMenu("extra","table", $module, $tool);
  print("</TABLE>");
}

